package project.dade.cn.bst.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.daimajia.androidanimations.library.Techniques;
import com.daimajia.androidanimations.library.YoYo;
import com.squareup.okhttp.Request;

import java.util.HashMap;
import java.util.Map;

import butterknife.InjectView;
import project.dade.cn.bst.R;
import project.dade.cn.bst.base.ZJPBaseBarActivity;
import project.dade.cn.bst.manager.ApiConfig;
import project.dade.cn.bst.model.BaseRequestsModel;
import project.dade.cn.bst.network.FormatRequest;
import project.dade.cn.bst.network.callback.ResultCallback;
import project.dade.cn.bst.network.request.OkHttpRequest;
import project.dade.cn.bst.utils.AppUtil;
import project.dade.cn.bst.utils.LogUtil;
import project.dade.cn.bst.utils.NetRequestUtil;
import project.dade.cn.bst.utils.ToastUtil;
import project.dade.cn.bst.utils.ValidateUtil;
import project.dade.cn.bst.views.ClearEditText;


/**
 * 找回密码
 */
public class FindPassActivity extends ZJPBaseBarActivity implements View.OnClickListener {

    @InjectView(R.id.phone)
    ClearEditText phone;
    @InjectView(R.id.submitBtn)
    AppCompatButton submitBtn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_findpass);
        initSetListener();
    }



    private void initSetListener() {
        submitBtn.setOnClickListener(this);
    }





    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.submitBtn:
                AppUtil.closeSoftInput(this);
                //数据验证
                if (isCheckInput()) {
                    requestFindPass();
                }
                break;

            case R.id.universal_tool:
                this.finish();
                break;




        }

    }

    private void requestFindPass() {
        HashMap<String,String > hashMap = new HashMap<String,String>();
        hashMap.put("MobilePhone",phone.getText().toString().trim());
        Map<String, String> params = new HashMap<String, String>();
        params.put("MobilePhone", phone.getText().toString().trim());
        params.put("Sign", FormatRequest.main(hashMap));
        new OkHttpRequest.Builder()
                .url(ApiConfig.getHostAddress()+ApiConfig.SERVER_URL.phone).params(params)
                .post(new ResultCallback<String>(){
                    @Override
                    public void onError(Request request, Exception e) {
                        ToastUtil.showShort(FindPassActivity.this, "网络相应失败请稍后再试");
                    }
                    @Override
                    public void onResponse(String response) {
                        LogUtil.i(TAG, "获取登录用户ID：" + response);
                        if (TextUtils.isEmpty(NetRequestUtil.requestsError(response, "找回密码"))) {
                            BaseRequestsModel model = JSON.parseObject(response,BaseRequestsModel.class);

                            if (model != null && model.getResult() == 0) {
                                Intent mIntentRegInfo = new Intent(FindPassActivity.this, FindPassPhoneVerActivity.class);
                                mIntentRegInfo.putExtra("phone", phone.getText().toString().trim());
                                startActivity(mIntentRegInfo);
                                finish();
                            }
                    }else {
                            ToastUtil.showShort(FindPassActivity.this, NetRequestUtil.requestsError(response, "找回密码"));

                        }
                    }

                }) ;
    }


    private boolean isCheckInput() {

        //账号不能为空
        if (TextUtils.isEmpty(phone.getText().toString().trim())) {
            ToastUtil.showShort(this, R.string.tips_phone_right);
            YoYo.with(Techniques.Shake)
                    .playOn(findViewById(R.id.phone));
            return false;
        }
        //验证手机号码
        if (!ValidateUtil.isMobile(phone.getText().toString().trim())) {
            ToastUtil.showShort(this, R.string.tips_phone_right);
            YoYo.with(Techniques.Shake)
                    .playOn(findViewById(R.id.phone));
            return false;
        }

        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            this.finish();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}
